package cn.chrilwe.pigpig.user.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.chrilwe.pigpig.core.model.user.Authority;
import cn.chrilwe.pigpig.core.model.user.Role;
import cn.chrilwe.pigpig.core.model.user.User;
import cn.chrilwe.pigpig.core.model.user.ext.UserExt;
import cn.chrilwe.pigpig.user.common.CheckPasswordEnum;
import cn.chrilwe.pigpig.user.common.ResponseEnum;
import cn.chrilwe.pigpig.user.mapper.AuthorityMapper;
import cn.chrilwe.pigpig.user.mapper.RoleMapper;
import cn.chrilwe.pigpig.user.mapper.UserMapper;
import cn.chrilwe.pigpig.user.response.UpdatePasswordResult;

/**
 * 
 * @author chrilwe
 * 2020-8-9
 */
@Service
@Transactional
public class UserService {

	@Autowired
	private UserMapper userMapper;
	
	@Autowired
	private RoleMapper roleMapper;
	
	@Autowired
	private AuthorityMapper authorityMapper;
	
	public UserExt findUserExtByUsername(String username) {
		User user = userMapper.selectUserByUsername(username);
		
		if(user != null) {
			UserExt userExt = new UserExt();
			
			List<Role> roleList = roleMapper.selectRolesByUserId(user.getId());
			userExt.setUser(user);
			if(roleList != null && roleList.size() > 0) {
				userExt.setRoles(roleList);
				List<Integer> roleIdList = new ArrayList<Integer>();
				for (Integer integer : roleIdList) {
					roleIdList.add(integer);
				}
				List<Authority> authList = authorityMapper.selectByRoleIds(roleIdList);
				userExt.setAuthorities(authList);
			}
			return userExt;
		}
		
		return null;
	}
	
	public void addUser(User user) {
		
	}
	
	protected boolean checkUser(User user) {
		if(user != null) {
			String username = user.getUsername();
			String password = user.getPassword();
			String phone = user.getPhone();
			if(StringUtils.isAnyEmpty(username, password, phone)) {
				
			}
		}
		return false;
	}

	
	public UpdatePasswordResult updatePassword(String oldPassword, String newPassword, String username) {
		if(StringUtils.isAnyEmpty(oldPassword, newPassword, username)) {
			return new UpdatePasswordResult(ResponseEnum.PARAM_ERR.getCode(), ResponseEnum.PARAM_ERR.getName());
		}
		
		User user = userMapper.selectUserByUsername(username);
		if(user == null) {
			return new UpdatePasswordResult(ResponseEnum.USERNAME_NOTEXISTS.getCode(), ResponseEnum.USERNAME_NOTEXISTS.getName());
		}
		String checkPassword = checkPassword(oldPassword, newPassword, user.getPassword());
		if(checkPassword.equals(CheckPasswordEnum.SUCCESS.getName())) {
			doUpdatePassword(username, newPassword);
			return  new UpdatePasswordResult(ResponseEnum.SUCCESS.getCode(),ResponseEnum.SUCCESS.getName());
		}
		return new UpdatePasswordResult(ResponseEnum.FAIL.getCode(),checkPassword);
	}
	
	protected String checkPassword(String oldPassword, String newPassword, String selectPassword) {
		BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
		String encode = passwordEncoder.encode(oldPassword);
		if(!encode.equals(selectPassword)) {
			return CheckPasswordEnum.OLD_PASSWORD_ERR.getName();
		}
		if(encode.equals(selectPassword)) {
			return CheckPasswordEnum.OLD_PASSWORD_NEW_PASSWORD_SAME.getName();
		}
		return  CheckPasswordEnum.SUCCESS.getName();
	}
	
	protected void doUpdatePassword(String username, String password) {
		BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
		String encode = passwordEncoder.encode(password);
		User user = new User();
		user.setPassword(encode);
		user.setUsername(username);
		userMapper.updateUser(user);
	}
}
